package com.pengpai.pangucms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pengpai.pangucms.entity.Qrcode;
import com.pengpai.pangucms.entity.QrcodeScanRecord;
import com.pengpai.pangucms.service.IQrcodeScanRecordService;
import com.pengpai.pangucms.service.IQrcodeService;
import com.pengpai.pangucms.utils.IPUtils;
import com.pengpai.pangucms.utils.UserAgentUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author rxg
 * @since 2023-08-31
 */
@Slf4j
@Controller
@RequestMapping("/qrcodeScanRecord")
public class QrcodeScanRecordController {

    @Autowired
    private IQrcodeService qrcodeService;

    @Autowired
    private IQrcodeScanRecordService qrcodeScanRecordService;

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/jumpTo/{uuid}")
    public void jumpTo(@PathVariable String uuid, HttpServletResponse response,HttpServletRequest request) throws IOException {
        log.info("获取二维码{}", uuid);

        //处理 扫描事件，IP，扫描设备

        //模拟从数据库根据 uuid 查询 jumpto
        QueryWrapper<Qrcode> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uuid",uuid);
        Qrcode qrcode = qrcodeService.getBaseMapper().selectOne(queryWrapper);
        LinkedHashMap<String,Object> location = restTemplate.getForObject("https://apis.map.qq.com/ws/location/v1/ip?ip="+IPUtils.getIpAddr(request)+"&key=P52BZ-J42EJ-7FNFC-DAVNU-356S7-CPFI7",LinkedHashMap.class);
        Object messagge= location.get("message");
        QrcodeScanRecord qrcodeScanRecord = new QrcodeScanRecord();
        qrcodeScanRecord.setQrId(qrcode.getQrId());
        qrcodeScanRecord.setSacnTime(LocalDateTime.now());
        qrcodeScanRecord.setSacnIp(IPUtils.getIpAddr(request));
        qrcodeScanRecord.setScanDevice(UserAgentUtils.getDevice(request));
        qrcodeScanRecordService.save(qrcodeScanRecord);
        String jumpTo = qrcode.getJumpTo();
        response.sendRedirect(jumpTo);
    }

    @GetMapping("/chart")
    @ResponseBody
    public List<Map<String,Object>> piechart(Qrcode qrcode){
        if (qrcode.getQrId()==null){
            QueryWrapper<QrcodeScanRecord> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.select("DISTINCT scan_device").lambda();
            List<QrcodeScanRecord> devices = qrcodeScanRecordService.getBaseMapper().selectList(queryWrapper1);
            List<String> deviceLiset= new ArrayList<>();
            for (QrcodeScanRecord qrcodeScanRecord :devices){
                deviceLiset.add(qrcodeScanRecord.getScanDevice());
            }
            List<Map<String,Object>> chartDetail = new ArrayList<>();

            for(String device : deviceLiset){
                QueryWrapper<QrcodeScanRecord> queryWrapper2 = new QueryWrapper<>();
                Map<String, Object> map = new HashMap<>();
                queryWrapper2.eq("scan_device",device);
                Integer count = Math.toIntExact(qrcodeScanRecordService.getBaseMapper().selectCount(queryWrapper2));
                map.put("value",count);
                map.put("name",device);
                chartDetail.add(map);
            }
            return chartDetail;
        }else {
            Integer qrId = qrcode.getQrId();
            QueryWrapper<QrcodeScanRecord> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("qr_id",qrId);
            queryWrapper1.select("DISTINCT scan_device").lambda();
            List<QrcodeScanRecord> devices = qrcodeScanRecordService.getBaseMapper().selectList(queryWrapper1);
            List<String> deviceLiset= new ArrayList<>();
            for (QrcodeScanRecord qrcodeScanRecord :devices){
                deviceLiset.add(qrcodeScanRecord.getScanDevice());
            }
            List<Map<String,Object>> chartDetail = new ArrayList<>();

            for(String device : deviceLiset){
                QueryWrapper<QrcodeScanRecord> queryWrapper2 = new QueryWrapper<>();
                Map<String, Object> map = new HashMap<>();
                queryWrapper2.eq("qr_id",qrId);
                queryWrapper2.eq("scan_device",device);
                Integer count = Math.toIntExact(qrcodeScanRecordService.getBaseMapper().selectCount(queryWrapper2));
                map.put("value",count);
                map.put("name",device);
                chartDetail.add(map);
            }
            return chartDetail;
        }

    }


    @GetMapping("location")
    @ResponseBody
    public String ipLocation(){
        restTemplate.getForObject("https://apis.map.qq.com/ws/location/v1/ip?ip=36.154.131.250&key=P52BZ-J42EJ-7FNFC-DAVNU-356S7-CPFI7",Object.class);
        log.info("获取二维码{}", restTemplate.getForObject("https://apis.map.qq.com/ws/location/v1/ip?ip=36.154.131.250&key=P52BZ-J42EJ-7FNFC-DAVNU-356S7-CPFI7",Object.class));
        log.info("获取二维码{}", restTemplate.getForObject("https://apis.map.qq.com/ws/location/v1/ip?ip=127.0.0.1&key=P52BZ-J42EJ-7FNFC-DAVNU-356S7-CPFI7",Object.class));
        QrcodeScanRecord qrcodeScanRecord = new QrcodeScanRecord();
        qrcodeScanRecord.setId(1);
        qrcodeScanRecord.setQrId(333);
        log.info("qr",qrcodeScanRecord);
        return null;
    }


}
